package com.nanfang51g3.ckz.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.event.AvatarEvent;
import com.nanfang51g3.ckz.event.RxBus;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.accounting.AccountingActivity;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.money.fill.FillInActivity;
import com.nanfang51g3.ckz.ui.person.PersonActivity;
import com.nanfang51g3.ckz.ui.update.password.UpPasswordActivity;
import com.nanfang51g3.ckz.ui.webView.PublicWebView;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.utils.ToolUtils;
import com.nanfang51g3.ckz.widget.GlideCircleTransform;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by Administrator on 2016/4/19.
 * * 版本1.0.0功能的类，已弃用[HomeMeFragment代替]
 */
public class MeActivity extends Base2Activity implements MeContract.View {
    MeContract.MeActionsListener meActionsListener;
    String url = "http://ckt.51g3.com/aboutapp.html";
    @Bind(R.id.me_iv_avatar)
    ImageView meIvAvatar;
    @Bind(R.id.tv_me_name)
    TextView tvMeName;
    @Bind(R.id.tv_me_phone)
    TextView tvMePhone;
    @Bind(R.id.tv_me_version)
    TextView tvMeVersion;
    String versionName;
    String versionCode;

    @Override
    public int getContentView() {
        return R.layout.me;
    }
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_me;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        String username = (String) SPUtil.get(this, "username", "请填写昵称");
        KLog.i("Evd", "usernmae=" + username + "-" + ToolUtils.isZN(username));
        String cont = "cKTcKT_";
        if (username.contains(cont)) {
            tvMeName.setText("请填写昵称");
        } else {
            tvMeName.setText(username);
        }


        String phone = (String) SPUtil.get(this, "phone", "暂未填写手机");
        tvMePhone.setText(phone);
        versionName = (String) SPUtil.get(this, "version_name", "1.0");
        versionCode = (String) SPUtil.get(this, "version_code", "1");
        tvMeVersion.setText(versionName);
    }

    Subscription rxSubscription;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        meActionsListener = DaggerMeContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .mePresenterModule(new MePresenterModule(this))
                .build().getMePresenter();
        KLog.i("TAG", meActionsListener == null ? "meActionsListener==null" : "meActionsListener!=null");
        ButterKnife.bind(this);
        String userpic = (String) SPUtil.get(this, "userpic", "");
        Glide.with(this).load(userpic).placeholder(R.mipmap.ic_default_avatar).transform(
                new GlideCircleTransform(this)
        ).into(meIvAvatar);
        rxSubscription = RxBus
                .getDefault()
                .toObservable(AvatarEvent.class)
                .subscribe(new Action1<AvatarEvent>() {
                    @Override
                    public void call(AvatarEvent file) {
                        KLog.i("Evd", "call - AvatarEvent" + file.getUrlAvatar());
                        Glide.with(MeActivity.this).load(file.getUrlAvatar()).placeholder(R.mipmap.ic_default_avatar).transform(
                                new GlideCircleTransform(MeActivity.this)
                        ).into(meIvAvatar);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        KLog.i("Evd", "call - throwable");
                    }
                });


    }

    @OnClick(R.id.relative_me_person)
    public void relative_me_person(View view) {
        startActivity(new Intent(this, PersonActivity.class));
    }

    @OnClick(R.id.relative_me_money)
    public void relative_me_money(View view) {
        startActivity(new Intent(this, AccountingActivity.class));
    }

    @OnClick(R.id.relative_me_password)
    public void relative_me_password(View view) {
        startActivity(new Intent(this, UpPasswordActivity.class));
    }

    @OnClick(R.id.relative_me_ti_xian)
    public void relative_me_ti_xian(View view) {
        startActivity(new Intent(this, FillInActivity.class));

    }

    @OnClick(R.id.relative_me_about)
    public void relative_me_about(View view) {
        startActivity(new Intent(this, PublicWebView.class).putExtra("id_url", url).putExtra("id_title", R.string.str_me_about));
//        startActivity(new Intent(this, TestActivity.class));
    }

    @OnClick(R.id.relative_me_version)
    public void relative_me_version(View view) {
//        EasyToast.showShort(this,"old旧的版本");
//        EasyToast.showShort(this,"new新的版本");
//        startActivity(new Intent(this, HomeMainActivity.class));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
